#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年5月20日

@author: chenyongbing
'''
import sys,os,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

sys.path.append(os.path.join(current_dir,'../../'))
from base.zhimind_datas import myZhimindDatas
from base.retail_datas import myRetailDatas

daylist = [['2014-10-01','2014-10-31'],
           ['2014-11-01','2014-11-30'],
           ['2014-12-01','2014-12-31'],
           ['2015-01-01','2015-01-31'],
           ['2015-02-01','2015-02-28'],
           ['2015-03-01','2015-03-31'],
           ['2015-04-01','2015-04-30']]
# daylist = [
#            ['2015-01-01','2015-01-31'],
#            ['2015-03-01','2015-03-31'],
#            ['2015-04-01','2015-04-30']]

import logging

logging.basicConfig(level=logging.DEBUG)

fr = open('../report/analyze_local_datas.txt','w')

groupInfos = myRetailDatas.get_all_groups_info()


# fr.write('GroupInfos=%s\n'%str(groupInfos))
groups = ['32010410','32012151','32010341','32013115','32013138','32010365','32010249','32010511']



for startTime , endTime in daylist:
    pass
    #全部
    print startTime,'-',endTime
    st = re.sub('-','',startTime)
    cycle_datas = myZhimindDatas.get_visit_cycle_from_mac_customer_stats(groups=groups,st=startTime,et=endTime)
    no_apple_cycle_datas  = myZhimindDatas.get_visit_cycle_from_mac_customer_stats(groups=groups, st=startTime,et=endTime,dis_factory='Apple',mac_type='global')
    global_cycle_datas  = myZhimindDatas.get_visit_cycle_from_mac_customer_stats(groups=groups, st=startTime,et=endTime,mac_type='global')
    fr.write('CycleDatas_%s=%s\n'%(st,str(cycle_datas)))
    fr.write('NoAppleCycleDatas__%s=%s\n'%(st,str(no_apple_cycle_datas)))
    fr.write('GlobalCycleDatas__%s=%s\n'%(st,str(global_cycle_datas)))
    
    
    continue
    
    #入店人数
    customer_cnt = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( role=0, distinct=True,  st=startTime, et=endTime)
    no_apple_customer_cnt = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( role=0, distinct=True,  st=startTime, et=endTime,dis_factory='Apple',mac_type='global')
    global_customer_cnt = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( role=0, distinct=True,  st=startTime, et=endTime,mac_type='global')
    
    #老顾客人数
    old_customer_cnt = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( role=10, distinct=True,  st=startTime, et=endTime)
    no_apple_old_customer_cnt = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( role=10, distinct=True,  st=startTime, et=endTime,dis_factory='Apple',mac_type='global')
    global_old_customer_cnt = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( role=10, distinct=True,  st=startTime, et=endTime,mac_type='global')
    
    
    
    fr.write('CustomerCnt_%s=%s\n'%(st,str(customer_cnt)))
    fr.write('NoAppleCustomerCnt__%s=%s\n'%(st,str(no_apple_customer_cnt)))
    fr.write('GlobalCustomerCnt__%s=%s\n'%(st,str(global_customer_cnt)))
    
    fr.write('OldCustomerCnt_%s=%s\n'%(st,str(old_customer_cnt)))
    fr.write('NoAppleOldCustomerCnt__%s=%s\n'%(st,str(no_apple_old_customer_cnt)))
    fr.write('GlobalOldCustomerCnt__%s=%s\n'%(st,str(global_old_customer_cnt)))
    
    
    continue
    #客流量 
    flow_datas = myZhimindDatas.get_visit_cnt_by_group(st=startTime, et=endTime, groupType=4, distinct=False)
    print 'flow_datas:',len(flow_datas)
    #入店量,停留时长总和,深访
    target_datas = myZhimindDatas.get_same_target_from_mac_customer_stats(st=startTime,et=endTime, targetList=['cust_visit_cnt','sum_dur','deep_visit_cust_visit_cnt'])
    print 'target_datas:',len(target_datas)
    #来访周期
    cycle_datas = myZhimindDatas.get_avg_visit_cycle_from_mac_customer_stats( st=startTime,et=endTime)
    print 'cycle_datas:',len(cycle_datas)
    fr.write('FlowDatas_%s=%s\n'%(startTime,str(flow_datas)))
    fr.write('TargetDatas_%s=%s\n'%(startTime,str(target_datas)))
    fr.write('CycleDatas_%s=%s\n'%(startTime,str(cycle_datas)))


    #除去全部苹果数据
    
    #客流量 
    no_apple_flow_datas = myZhimindDatas.get_visit_cnt_by_group(st=startTime, et=endTime, groupType=4, distinct=False,dis_factory='Apple',mac_type='global')
    print 'no_apple_flow_datas:',len(no_apple_flow_datas)
    #入店量,停留时长总和,深访
    no_apple_target_datas = myZhimindDatas.get_same_target_from_mac_customer_stats(st=startTime,et=endTime, targetList=['cust_visit_cnt','sum_dur','deep_visit_cust_visit_cnt'],dis_factory='Apple',mac_type='global')
    print 'no_apple_target_datas:',len(no_apple_target_datas)
    #来访周期
    no_apple_cycle_datas  = myZhimindDatas.get_avg_visit_cycle_from_mac_customer_stats( st=startTime,et=endTime,dis_factory='Apple',mac_type='global')
    print 'no_apple_cycle_datas:',len(no_apple_cycle_datas)
    fr.write('NoAppleFlowDatas_%s=%s\n'%(startTime,str(no_apple_flow_datas)))
    fr.write('NoAppleTargetDatas_%s=%s\n'%(startTime,str(no_apple_target_datas)))
    fr.write('NoAppleCycleDatas_%s=%s\n'%(startTime,str(no_apple_cycle_datas)))
    
    #去除随机mac
    
    #客流量 
    global_flow_datas = myZhimindDatas.get_visit_cnt_by_group(st=startTime, et=endTime, groupType=4, distinct=False,mac_type='global')
    print 'global_flow_datas:',len(global_flow_datas)
    #入店量,停留时长总和,深访
    global_target_datas = myZhimindDatas.get_same_target_from_mac_customer_stats(st=startTime,et=endTime, targetList=['cust_visit_cnt','sum_dur','deep_visit_cust_visit_cnt'],mac_type='global')
    print 'global_target_datas:',len(global_target_datas)
    #来访周期
    global_cycle_datas = myZhimindDatas.get_avg_visit_cycle_from_mac_customer_stats( st=startTime,et=endTime,mac_type='global')
    print 'global_cycle_datas:',len(global_cycle_datas)
    fr.write('GlobalFlowDatas_%s=%s\n'%(startTime,str(global_flow_datas)))
    fr.write('GlobalTargetDatas_%s=%s\n'%(startTime,str(global_target_datas)))
    fr.write('GlobalCycleDatas_%s=%s\n'%(startTime,str(global_cycle_datas)))
fr.close()
if __name__ == '__main__':
    pass